package fr.umlv.jee.jawp.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import fr.umlv.jee.jawp.persistence.dao.IOrderDAO;
import fr.umlv.jee.jawp.persistence.object.Order;
import fr.umlv.jee.jawp.persistence.object.Product;
import fr.umlv.jee.jawp.persistence.object.Status;
import fr.umlv.jee.jawp.service.subscribe.MailEngine;

@Service("orderService")
public class OrderService {

  @Resource(name = "orderDAO")
  private IOrderDAO dao;

  /*************************************
   * Create
   *************************************/

  @Transactional
  public Order createNewOrder(String customer) {
    return dao.createNewOrder(customer);
  }

  /*************************************
   * Getters
   *************************************/

  @Transactional(readOnly = true)
  public List<Order> getOrders() {
    return dao.getOrders();
  }

  @Transactional(readOnly = true)
  public List<Order> getOrdersByCustomer(String customerId) {
    return dao.getOrdersByCustomer(customerId);
  }

  @Transactional(readOnly = true)
  public List<Order> getNonKartOrdersByCustomer(String customer) {
    return dao.getNonKartOrdersByCustomer(customer);
  }

  @Transactional(readOnly = true)
  public int getNbNonKartOrdersByCustomer(String customer) {
    return dao.getNonKartOrdersByCustomer(customer).size();
  }

  @Transactional(readOnly = true)
  public Order getOrderById(int id) {
    return dao.getOrderById(id);
  }

  @Transactional(readOnly = true)
  public List<Product> getProductByOrder(Order order) {
    return dao.getProductByOrder(order);
  }

  public long getNbProductByOrder(Order order) {
    return dao.getNbProductByOrder(order);
  }

  public long getNbOrderByCustomer(String customer) {
    return dao.getNbOrderByCustomer(customer);
  }

  /*************************************
   * Setter
   *************************************/

  @Transactional
  public void addProduct(Order o, Product p) {
    dao.addProduct(o, p);
  }

  @Transactional
  public void addProduct(Order o, String asin, ProductService p) {
    Product product = p.getProductByASIN(asin);
    dao.addProduct(o, product);
  }

  @Transactional
  public void removeProduct(Order o, Product p) {
    dao.removeProduct(o, p);
  }

  @Transactional
  public void saveOrder(Order order) {
    dao.saveOrder(order);
  }

  @Transactional
  public void saveOrders(List<Order> orders) {
    dao.saveOrders(orders);
  }

  @Transactional
  public void setStatus(Order order, Status status) {
    dao.setStatus(order, status);
  }

  @Transactional
  public void setStatus(Order order, int status) {
    List<Product> list = getProductByOrder(order);
    switch (status) {
      case 0:
        setStatus(order, Status.KART);
        break;
      case 1:
        if (list.size() != 0) {
          setStatus(order, Status.ORDERED);
          MailEngine mail = new MailEngine();
          mail.sendOrderMail(order, list);
        }
        break;
      case 2:
        setStatus(order, Status.PAYED);
        break;
      case 3:
        setStatus(order, Status.PREPARED);
        break;
      case 5:
        setStatus(order, Status.DELAYED);
        break;
      case 6:
        setStatus(order, Status.SENT);
        break;

      default:
        break;
    }
  }

  /*************************************
   * Deletion
   *************************************/

  @Transactional
  public void deleteOrder(int id) {
    dao.deleteOrder(id);
  }

  @Transactional
  public void deleteOrdersByCustomer(int customerId) {
    dao.deleteOrdersByCustomer(customerId);
  }

  @Transactional
  public void clearDB() {
    dao.clearDB();
  }
}
